package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.login.RequiresLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiongkunYang
 */
@Tag(name = "专辑管理")
@RestController
@RequestMapping("/api/album")
public class AlbumInfoApiController {

    private final AlbumInfoService albumInfoService;


    public AlbumInfoApiController(AlbumInfoService albumInfoService) {
        this.albumInfoService = albumInfoService;
    }

    @Operation(summary = "新增专辑")
    @Parameters({
            @Parameter(name = "albumInfoVo", description = "专辑信息", required = true)
    })
    @RequiresLogin
    @PostMapping("/albumInfo/saveAlbumInfo")
    public Result<Void> saveAlbumInfo(@RequestBody @Validated AlbumInfoVo albumInfoVo) {
        Long userId = AuthContextHolder.getUserId();
        albumInfoService.saveAlbumInfo(userId, albumInfoVo);
        return Result.ok();
    }

    @Operation(summary = "查看当前用户专辑分页列表")
    @Parameters({
            @Parameter(name = "page", description = "当前页码", required = true),
            @Parameter(name = "limit", description = "每页记录数", required = true),
            @Parameter(name = "albumInfoQuery", description = "专辑查询条件")
    })
    @RequiresLogin
    @PostMapping("/albumInfo/findUserAlbumPage/{page}/{limit}")
    public Result<IPage<AlbumListVo>> findUserAlbumPage(@PathVariable Long page,
                                                        @PathVariable  Long limit,
                                                        @RequestBody AlbumInfoQuery albumInfoQuery) {
        Long userId = AuthContextHolder.getUserId();
        albumInfoQuery.setUserId(userId);
        Page<AlbumListVo> albumListVoPage = new Page<>(page, limit);
        return Result.ok(this.albumInfoService.getUserAlbumPage(albumListVoPage, albumInfoQuery));
    }

    @Operation(summary = "根据ID删除专辑")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID", required = true)
    })
    @RequiresLogin
    @DeleteMapping("/albumInfo/removeAlbumInfo/{id}")
    public Result<Void> removeAlbumInfo(@PathVariable Long id) {
        this.albumInfoService.removeAlbumInfoById(id);
        return Result.ok();
    }

    @Operation(summary = "根据ID查询专辑信息")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID", required = true)
    })
    @GetMapping("/albumInfo/getAlbumInfo/{id}")
    public Result<AlbumInfo> getAlbumInfoById(@PathVariable Long id) {
        AlbumInfo albumInfo = this.albumInfoService.getAlbumInfoById(id);
        return Result.ok(albumInfo);
    }

    @Operation(summary = "根据ID更新专辑信息")
    @Parameters({
            @Parameter(name = "id", description = "专辑ID", required = true),
            @Parameter(name = "albumInfoVo", description = "专辑信息", required = true)
    })
    @RequiresLogin
    @PutMapping("/albumInfo/updateAlbumInfo/{id}")
    public Result<Void> updateAlbumInfo(@PathVariable Long id, @RequestBody AlbumInfoVo albumInfoVo) {
        albumInfoService.updateAlbumInfoById(id, albumInfoVo);
        return Result.ok();
    }

    @Operation(summary = "获取当前用户全部专辑列表")
    @RequiresLogin
    @GetMapping("/albumInfo/findUserAllAlbumList")
    public Result<List<AlbumInfo>> findUserAllAlbumList() {
        Long userId = AuthContextHolder.getUserId();
        List<AlbumInfo> albumInfoList = this.albumInfoService.getUserAllAlbumList(userId);
        return Result.ok(albumInfoList);
    }

    // /api/album/albumInfo/getAlbumStatVo/{albumId}
    @Operation(summary = "根据专辑ID获取专辑统计信息")
    @Parameters({
            @Parameter(name = "albumId", description = "专辑ID", required = true)
    })
    @GetMapping("/albumInfo/getAlbumStatVo/{albumId}")
    Result<AlbumStatVo> getAlbumStatVo(@PathVariable Long albumId) {
        AlbumStatVo albumStatVo = this.albumInfoService.getAlbumStatVoByAlbumId(albumId);
        return Result.ok(albumStatVo);
    }
}

